<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>缺陷管理>>缺陷类型设置</title>

    <link rel="stylesheet" href="../layui/layui.js"  media="all">
    <link rel="stylesheet" href="../layui/layui.all.js"  media="all">
   <!-- <script src="../layui/layui.all.js"></script>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-2.0.3.min.js"></script>-->
</head>
<body>

<!--缺陷管理>>缺陷类型设置-->


<!--加面板-->
<div style="padding:10px;background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">缺陷管理>>缺陷类型设置</div>
                <div class="layui-card-body">
                    <!--数据表格-->
                    <table class="layui-hide" id="defect_table" lay-filter="defect_table"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--选中区域-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add_defect">添加缺陷类型</button>
    </div>
</script>

<!--表格中操作：编辑、删除-->
<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-xs" lay-event="defect_edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="defect_del">删除</a>
</script>


<script type="text/javascript">
    $(function () {
        var defect = sessionStorage.getItem("defect");
        if(defect != null){
            defect = JSON.parse(defect);
            $("#emp_name").text(defect.emp_name);
        }else{
//            window.location.href = "/login.html";
        }
    });
</script>

<script>
    layui.use(['form','upload','laydate','layer'], function(){
        var $ = layui.jquery
            ,upload = layui.upload
            ,laydate = layui.laydate
            ,layedit = layui.layedit
            ,layer=layui.layer
            ,form = layui.form;
    layui.use('table', function() {
        var table = layui.table;

        table.render({
            elem: '#defect_table'
            , url: '/queryDefectType.do'
            , toolbar: '#toolbarDemo'
            ,title:'缺陷类型表'
            , totalRow: false
            , cols: [[
                {field: 'defect_id', title: '缺陷类型名称' ,style:'display:none' }
                 ,{field: 'defect_name', title: '缺陷类型名称',align:'center',width: 550}
                ,{field: 'state_name', title: '状态(启用/未启用)',align:'center',width: 450}
                , {fixed: 'right', title: '操作', toolbar: '#operation',width: 250,align:'center',templet:function(row){
                    }},
            ]]
            , page: {
                limits: [5, 10, 15, 20]
            },
            done: function (obj) {
                $("[data-field='defect_id']").css('display','none');
                layer.closeAll();
               /* form.on('checkbox(checkboxIsSelected)', function(data){
                    var _index = $(data.elem).attr('table-index')||0;
                    if(data.elem.checked){
                        obj.data[_index].is_qualified = data.value;
                    }else{
                        obj.data[_index].is_qualified = 2;
                    }
                });*/

            }

        });

        //头工具栏事件
        table.on('toolbar(defect_table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add_defect':
                    var index=layer.open({
                        type: 2,
                        title: ['添加缺陷类型 ', 'font-size:13px;background-color: #269ce9;color:white;text-algin:center'],
                        shade: [0.7, 'rgba(37,31,36,0.79)'],
                        area: ['50%', '40%'],
                        content:'/pages/add_defectType.html'
                        ,end:function () {
                            //执行重载
                            table.reload('defect_table', {
                                where: {
                                    key: {
                                        time: new Date()
                                    }
                                }
                            }, 'data');
                            layer.close(index);
                        }
                    });


            }
        });
        //监听行工具事件
        table.on('tool(defect_table)', function(obj){
            var data = obj.data;
            if(obj.event === 'defect_del'){
                layer.confirm('真的删除行么', function(index){
                    var defect_id = data.defect_id;
                    var state_name=data.state_name;
                    $.ajax({
                        url: "/defectDel.do",
                        type: "post",
                        data: {defect_id:defect_id,state_name:state_name},
                        dataType: "json",
                        success:function(result){
                            if(result.code == 200){//删除成功
                                obj.del();
                                //执行重载
                                table.reload('defect_table', {
                                    where: {
                                        key: {
                                            time: new Date()
                                        }
                                    }
                                }, 'data');
                                layer.close(index);
                            }
                            //出现异常等其他情况
                            if(result.code == 501){
                                alert(result.message);
                                layer.close(index);
                            }
                        }
                    })
                });
            } else if(obj.event === 'defect_edit'){
                console.log("defect_edit"+data.field);
                var index=layer.open({
                    type: 2,
                    title: ['修改缺陷类型', 'font-size:13px;background-color: #269ce9;color:white;text-algin:center'],
                    shade: [0.7, 'rgba(37,31,36,0.79)'],
                    content: '/pages/update_defectType.html',
                    area: ['50%', '50%'],
                        success:function(layero,index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        body.find("#defect_id").val(data.defect_id);
                        body.find("#defect_name").val(data.defect_name);
                        body.find("#state_name").val(data.state_name);
                    },end:function () {
                        //执行重载
                        table.reload('defect_table', {
                            where: {
                                key: {
                                    time: new Date()
                                }
                            }
                        }, 'data');
                        layer.close(index);
                    }
                });
            }
        });
    });
    });
</script>
</body>
</html>